﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Imaginecup.shareData;


namespace Imaginecup.util
{
    public interface IKeyboardInputHandler
    {
        KeyboardState CurrentKeyboardState { get; }
        KeyboardState PreviousKeyboardState { get; }
        bool IsOneKeyDown(Keys key);
        bool IsOneKeyUp(Keys key);
    };

    public class KeyboardInputHandler : GameComponent, IKeyboardInputHandler
    {

        private KeyboardState currentKeyboardState;
        private KeyboardState previousKeyboardState;

        private static KeyboardInputHandler m_InputHandler = null;

        private KeyboardInputHandler()
            : base(StateShareData.GetShareData().GetGame())
        {
            currentKeyboardState = Keyboard.GetState();
            base.Initialize();
        }

        public static KeyboardInputHandler GetInputHandler()
        {
            if (m_InputHandler == null)
            {
                m_InputHandler = new KeyboardInputHandler();
            }
            return m_InputHandler;
        }

        public bool IsOneKeyDown(Keys key)
        {
            if (PreviousKeyboardState.IsKeyUp(key) && CurrentKeyboardState.IsKeyDown(key))
            {
                return true;
            }
            return false;
        }
        public bool IsOneKeyUp(Keys key)
        {
            if (PreviousKeyboardState.IsKeyDown(key) && CurrentKeyboardState.IsKeyUp(key))
            {
                return true;
            }
            return false;
        }

        public sealed override void Update(GameTime gameTime)
        {
            previousKeyboardState = currentKeyboardState;
            currentKeyboardState = Keyboard.GetState();
            base.Update(gameTime);
        }

        public KeyboardState CurrentKeyboardState
        {
            get { return (currentKeyboardState); }
        }
        public KeyboardState PreviousKeyboardState
        {
            get { return (previousKeyboardState); }
        }

    }
}
